package module

import (
    "github.com/gin-gonic/gin"
    "yi-shou-backstage/errmsg"
    "yi-shou-backstage/model"
    "yi-shou-backstage/util"
    "yi-shou-backstage/module/result"
)

func CreateChannelControl(req *gin.Context) (int, string, interface{}) {
    // 获取参数
    var params model.Channel
    err := req.ShouldBindJSON(&params)
    if err != nil {
        util.LogEntry.Errorf("参数格式错误, error = %s, param = %s \n", err, params)
        return errmsg.PARAM_FORMAT_ERROR, errmsg.GetErrorByCode(errmsg.PARAM_FORMAT_ERROR), nil
    }

    // 校验参数
    if ok, msg := util.Validator(&params); !ok {
        util.LogEntry.Errorf("参数格式错误, error = %s, param = %s \n", err, params)
        return errmsg.PARAM_FORMAT_ERROR, msg, nil
    }

    // 保存数据
    data := model.CreateChannel(params)
    if data == nil {
        util.LogEntry.Errorf("创建文章频道失败 \n")
        return errmsg.CREATE_CHANNEL_ERROR, errmsg.GetErrorByCode(errmsg.CREATE_CHANNEL_ERROR), nil
    }
    return errmsg.SUCCESS, errmsg.GetErrorByCode(errmsg.SUCCESS), data
}

func ListChannelsControl(req *gin.Context) (int, string, interface{}) {
    // 查询数据
    total, rows := model.ListChannels()
    if rows == nil {
        util.LogEntry.Errorf("获取文章频道失败 \n")
        return errmsg.LIST_CHANNELS_ERROR, errmsg.GetErrorByCode(errmsg.LIST_CHANNELS_ERROR), nil
    }

    data := result.ListChannelsResponse{TotalCount: total, Channels: rows, ID: rows[0].ID, Name: rows[0].Name}
    return errmsg.SUCCESS, errmsg.GetErrorByCode(errmsg.SUCCESS), data
}